package cn.hiapi.shop.api;

import cn.hiapi.basic.ResUtils;
import cn.hiapi.basic.controller.BasicQueryController;
import cn.hiapi.basic.security.TokenGet;
import cn.hiapi.basic.service.BasicService;
import cn.hiapi.finance.api.query.IncomeQuery;
import cn.hiapi.finance.entity.Income;
import cn.hiapi.finance.service.IncomeService;
import cn.hiapi.finance.vo.IncomeVo;
import cn.hiapi.interfaces.DispatchContext;
import cn.hiapi.shop.query.ShopIncomeQuery;
import cn.hiapi.shop.service.shop.ShopLinkUserService;
import cn.hiapi.shopkeeper.consts.AppConst;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author AdinZ
 * @date 2022/12/13 23:05
 */
@RestController("shopkeeperIncomeController")
@RequestMapping("/shop/income")
public class IncomeController extends BasicQueryController<Income, Long, IncomeVo, ShopIncomeQuery> {
    protected IncomeController(IncomeService basicService, DispatchContext dispatchContext) {
        super(basicService, dispatchContext);
    }


    @Override
    public ShopIncomeQuery setQuery(ShopIncomeQuery query) {
        query.setFid(query.getShopId());
        this.dispatchContext.getServiceOne(ShopLinkUserService.class).verifyShop(TokenGet.get().getFid(), query.getShopId(), AppConst.SHOP_ROLE_AMOUNT);
        return super.setQuery(query);
    }

    @Override
    public List<IncomeVo> toListVo(List<Income> list, String[] fields) {
        return list.stream().map(e -> {
            IncomeVo vo = e.copy(new IncomeVo());
            vo.setAmount(ResUtils.decimal(e.getAmount(), 2));
            vo.setBeforeAmount(ResUtils.decimal(e.getBeforeAmount(), 2));
            vo.setAfterAmount(ResUtils.decimal(e.getAfterAmount(), 2));
            return vo;
        }).collect(Collectors.toList());
    }

    @Override
    public IncomeVo toData(Income e, String[] fields) {
        IncomeVo vo = e.copy(new IncomeVo());
        return vo;
    }
}
